package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.constant.SystemConstants;
import com.settlement.system.common.enums.PlatFormEnum;
import com.settlement.system.common.model.Option;
import com.settlement.system.mapper.SysBillLockMapper;
import com.settlement.system.mapper.SysStoreMapper;
import com.settlement.system.model.entity.SysBillLock;
import com.settlement.system.model.entity.SysStore;
import com.settlement.system.model.form.BillLockForm;
import com.settlement.system.model.query.BillLockPageQuery;
import com.settlement.system.service.SysBillLockService;
import com.settlement.system.service.SysDictTypeService;
import com.settlement.system.service.SysStoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 核销锁账表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2023-08-25
 */
@Service
@RequiredArgsConstructor
public class SysBillLockServiceImpl extends ServiceImpl<SysBillLockMapper, SysBillLock> implements SysBillLockService {

    private final SysDictTypeService dictTypeService;
    private final SysStoreMapper storeMapper;

    @Override
    public Page<SysBillLock> getBillLockPage(BillLockPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();
        // 所有店铺
        List<SysStore> storeList = storeMapper.selectList(null);
        // 查询数据
        Page<SysBillLock> page =  this.page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<SysBillLock>().like(StrUtil.isNotBlank(keywords), SysBillLock::getCommercePlatformStoreCode, keywords));
        List<SysBillLock> records = page.getRecords();
        if (records.size() > 0) {
            List<Option> options = dictTypeService.listDictItemsByTypeCode("needAudit");
            List<Option> platformList = dictTypeService.listDictItemsByTypeCode(SystemConstants.PLATFORM_KEY);
            for (SysBillLock record : records) {
                record.setAuditTypeName(options.stream().filter(option -> option.getValue().equals(record.getAuditType())).findFirst().map(Option::getLabel).orElse(null));
                record.setCommercePlatformStoreName(storeList.stream().filter(store -> store.getCommercePlatformStoreCode().equals(record.getCommercePlatformStoreCode())).findFirst().map(SysStore::getCommercePlatformStoreName).orElse(null));
                storeList.stream().filter(store -> store.getCommercePlatformStoreCode().equals(record.getCommercePlatformStoreCode())).findFirst().ifPresent(store -> {
                    platformList.stream().filter(platform -> platform.getValue().equals(store.getStoreKey())).findFirst().ifPresent(platform -> {
                        record.setCommercePlatformStoreCode(platform.getLabel());
                    });
                });
            }
        }
        return page;
    }

    @Override
    public SysBillLock getBillLockFormData(Long billLockId) {
        SysBillLock billLock = this.baseMapper.selectById(billLockId);
        if (billLock != null) {
            SysStore store = storeMapper.selectOne(new LambdaQueryWrapper<SysStore>().eq(SysStore::getCommercePlatformStoreCode, billLock.getCommercePlatformStoreCode()));
            if (store != null) {
                billLock.setCommercePlatformStoreName(store.getCommercePlatformStoreName());
            }
        }
        return billLock;
    }

    @Override
    public boolean saveBillLock(BillLockForm billLockForm) {
        // 实体转换 form->entity
        SysBillLock store = new SysBillLock();
        BeanUtil.copyProperties(billLockForm, store);
        List<SysBillLock> sysBillLocks = this.list(new LambdaQueryWrapper<SysBillLock>().eq(SysBillLock::getCommercePlatformStoreCode, billLockForm.getCommercePlatformStoreCode()));
        if (sysBillLocks.size() > 0) {
            BeanUtil.copyProperties(billLockForm, sysBillLocks.get(0), "id");
            return saveOrUpdate(sysBillLocks.get(0));
        } else {
            return saveOrUpdate(store);
        }
    }

    @Override
    public boolean deleteBillLock(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的核销锁账数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean deleteByCodeList(List<String> codeList) {
        List<SysBillLock> sysBillLocks = this.list(new LambdaQueryWrapper<SysBillLock>().in(SysBillLock::getCommercePlatformStoreCode, codeList));
        for (SysBillLock sysBillLock : sysBillLocks) {
            this.baseMapper.deleteById(sysBillLock);
        }
        return true;
    }
}
